//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "InflationCouponPricer.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Cashflows/InflationCoupon.h>
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CInflationCouponPricer::CInflationCouponPricer (boost::shared_ptr<QuantLib::InflationCouponPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInflationCouponPricer = NULL;
#endif
	_ppInflationCouponPricer = &childNative;
    
}
Cephei::QL::Cashflows::CInflationCouponPricer::CInflationCouponPricer (QuantLib::InflationCouponPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phInflationCouponPricer = NULL;
#endif
	_ppInflationCouponPricer = new boost::shared_ptr<QuantLib::InflationCouponPricer> (&childNative);
    
    _InflationCouponPricerOwner = owner;
    
}

Cephei::QL::Cashflows::CInflationCouponPricer::CInflationCouponPricer (CInflationCouponPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInflationCouponPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInflationCouponPricer = new boost::shared_ptr<QuantLib::InflationCouponPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Cashflows::CInflationCouponPricer::CInflationCouponPricer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInflationCouponPricer = NULL;
#endif
	if (!t->IsSubclassOf(CInflationCouponPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CInflationCouponPricer::CInflationCouponPricer (QuantLib::Handle<QuantLib::InflationCouponPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInflationCouponPricer = &childNative;
	_ppInflationCouponPricer = &static_cast<boost::shared_ptr<QuantLib::InflationCouponPricer>>(childNative.currentLink());
    
    _InflationCouponPricerOwner = owner;
}
Cephei::QL::Cashflows::CInflationCouponPricer::CInflationCouponPricer (QuantLib::Handle<QuantLib::InflationCouponPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInflationCouponPricer = &childNative;
	_ppInflationCouponPricer = &static_cast<boost::shared_ptr<QuantLib::InflationCouponPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CInflationCouponPricer::CInflationCouponPricer (QuantLib::InflationCouponPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInflationCouponPricer = NULL;
#endif
	_ppInflationCouponPricer = new boost::shared_ptr<QuantLib::InflationCouponPricer> (new QuantLib::InflationCouponPricer (childNative));
    
}
#endif

Cephei::QL::Cashflows::CInflationCouponPricer::~CInflationCouponPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppInflationCouponPricer != NULL)
    {
	    delete _ppInflationCouponPricer;
        _ppInflationCouponPricer = NULL;
    }
}
Cephei::QL::Cashflows::CInflationCouponPricer::!CInflationCouponPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppInflationCouponPricer != NULL)
    {
	    delete _ppInflationCouponPricer;
    }
}
QuantLib::InflationCouponPricer& Cephei::QL::Cashflows::CInflationCouponPricer::GetReference ()
{
    if (_ppInflationCouponPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppInflationCouponPricer;
}
boost::shared_ptr<QuantLib::InflationCouponPricer>& Cephei::QL::Cashflows::CInflationCouponPricer::GetShared ()
{
    if (_ppInflationCouponPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppInflationCouponPricer;
}
QuantLib::InflationCouponPricer* Cephei::QL::Cashflows::CInflationCouponPricer::GetPointer ()
{
    if (_ppInflationCouponPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppInflationCouponPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::InflationCouponPricer>& Cephei::QL::Cashflows::CInflationCouponPricer::GetHandle ()
{
	if (_phInflationCouponPricer == NULL)
	{
		_phInflationCouponPricer = new Handle<QuantLib::InflationCouponPricer> (*_ppInflationCouponPricer);
	}
	return *_phInflationCouponPricer;
}
#endif
bool Cephei::QL::Cashflows::CInflationCouponPricer::HasNative () 
{
	return (_ppInflationCouponPricer != NULL);
}

Double Cephei::QL::Cashflows::CInflationCouponPricer::CapletPrice (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInflationCouponPricer)->capletPrice ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CInflationCouponPricer::CapletRate (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppInflationCouponPricer)->capletRate ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CInflationCouponPricer::FloorletPrice (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInflationCouponPricer)->floorletPrice ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CInflationCouponPricer::FloorletRate (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppInflationCouponPricer)->floorletRate ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IInflationCouponPricer^ Cephei::QL::Cashflows::CInflationCouponPricer::Initialize (Cephei::QL::Cashflows::IInflationCoupon^ prm1)
{
    CInflationCoupon^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CInflationCoupon^> (prm1);
        _Cprm1->Lock();
        QuantLib::InflationCoupon& _prm1 = static_cast<QuantLib::InflationCoupon&> (_Cprm1->GetReference ()); 
    	(*_ppInflationCouponPricer)->initialize ( _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Double Cephei::QL::Cashflows::CInflationCouponPricer::SwapletPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInflationCouponPricer)->swapletPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CInflationCouponPricer::SwapletRate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppInflationCouponPricer)->swapletRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IInflationCouponPricer^ Cephei::QL::Cashflows::CInflationCouponPricer::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppInflationCouponPricer)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

